﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WodLib.Controls
{
    public partial class WodCollectionEditorForm<T> : Form
        where T:WodObject
    {
        private List<T> collection;

        public WodCollectionEditorForm(List<T> collection)
        {
            InitializeComponent();
            this.collection = collection;
        }

        private void WodCollectionEditorForm_Load(object sender, EventArgs e)
        {
            wodObjectBindingSource.DataSource = collection;
            WodLib.Tools.HierarchicalTypeFinder htf = new WodLib.Tools.HierarchicalTypeFinder();
            htf.FindTypes(typeof(T));
            foreach (var item in htf.TypesFound)
            {
                if (!item.IsAbstract)
                    typeBindingSource.Add(item);
            }
            this.Text = typeof(T).ToString() + " collection editor";
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            wodObjectBindingSource.Add(Activator.CreateInstance(
                (Type)typeBindingSource.Current));
        }

        private void wodObjectBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            propertyGridMain.SelectedObject = wodObjectBindingSource.Current;
            labelType.Text = propertyGridMain.SelectedObject.GetType().ToString();
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            wodObjectBindingSource.RemoveCurrent();
        }
    }
}
